/*
============================================================================
 Name        : BlockReadZXG.cpp
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : CBlockReadZXG implementation
============================================================================
*/

#include "BlockReadZXG.h"
#include <stringloader.h>
#include "eStockappui.h"
#include "eStockMainGridContainer.h"
#include "BlockGrid.h"
#include "BlockMemo.h"
#include <eStock_L2hy.rsg>
#include "ConfigFile.h"
#include "FolderUtils.h"

CBlockReadZXG::CBlockReadZXG(CBaseBlock* bsBack):
CBlock(bsBack)
{
}

CBlockReadZXG::~CBlockReadZXG()
{
	if(m_DelayTimer)
	{
		m_DelayTimer->Cancel();
		delete m_DelayTimer;
	}
	delete m_strText;
}

CBlockReadZXG* CBlockReadZXG::NewLC(CBaseBlock* bsBack)
{
	CBlockReadZXG* self = new (ELeave)CBlockReadZXG(bsBack);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CBlockReadZXG* CBlockReadZXG::NewL(CBaseBlock* bsBack)
{
	CBlockReadZXG* self=CBlockReadZXG::NewLC(bsBack);
	CleanupStack::Pop(); // self;
	return self;
}

void CBlockReadZXG::ConstructL()
{
	CBlock::ConstructL();
	
	m_strOK.Copy(KNullDesC);
	CGlobal::GetDesFromID(m_strCancel, LOC_BUF_MENU_CANCEL);
	m_strText = StringLoader::LoadL(LOC_BUF_STR_UPDATEZXGING);

	SizeChanged();
}

void CBlockReadZXG::SizeChanged()
{
	m_rect.SetRect(0, m_nScreenHeight-KScrollInfobarHeight-KCbabarHeight-m_nLineHeight*2, m_nScreenWidth, 
		m_nScreenHeight - KScrollInfobarHeight - KCbabarHeight);
	m_rect.Shrink(1,1);
}

void CBlockReadZXG::OnDraw(CFbsBitGc& g) const
{
	TInt nBottom = m_rect.iBr.iY-1;
	TInt nTop = m_rect.iTl.iY;
	TRgb rgbLightTop = TRgb(0x52,0x52,0x52);
	TRgb rgbLightBottom = TRgb(0x00,0x00,0x00);
	CGlobal::DrawGradientLines(g,m_rect.iTl,m_rect.iBr,rgbLightTop,rgbLightBottom);
	CGlobal::DrawRect(g,m_rect,m_pGlobal->g_rgbBorder/*,m_pGlobal->g_rgbBackGround*/);

	//g.SetPenColor(m_pGlobal->g_rgbHighlight);
	//TPoint titlePos = TPoint(m_pGlobal->m_TitlePosX, m_pGlobal->m_nSpaceUp+m_pGlobal->g_nFontHeight);
	//g.DrawText(m_strTitle, titlePos);

	CGlobal::DrawString(g, m_strText->Des(), m_rect.Width() / 2, m_rect.iTl.iY + m_nLineHeight / 2, CENTER|CENTER, m_pGlobal->g_rgbText);
}

void CBlockReadZXG::OnKeyEvent(const TInt& keyCode)
{
	if (keyCode==m_pGlobal->g_nKeyCancel && m_strCancel.Length() != 0)
	{
		GetContainer()->DelBlock();
		return;
	}
}

TBool CBlockReadZXG::ReadData(const TDesC8& aData)
{
	TPtrC8 ptr(aData);

	m_bSocketed = ETrue;
	m_bExit = ETrue;
	TInt nError =  CGlobal::ReadInt32L(ptr);
	HBufC* strError = CGlobal::ReadStringL(ptr);
	if (nError!=0)
	{
		GetContainer()->AddBlock(CBlockMemo::NewL(NULL, 100, strError->Des()));
		delete strError;
	}
	else
	{
		TConfigFile::WriteIniL(_L("zxgSynchronized_id"), m_pGlobal->m_strUserName, FolderUtils::ConfigDataFile());

		delete strError;
		CGlobal::ReadInt32L(ptr);//ServerZxgDate
		CGlobal::ReadInt32L(ptr);//ServerZxgTime

		TInt size = CGlobal::ReadInt32L(ptr);

		if(size > 0)
		{
			m_pGlobal->g_vZXG.Reset();
		}

		for (TInt i = 0; i<size; i++)
		{
			TInt id = CGlobal::ReadInt32L(ptr);
			HBufC* name = CGlobal::ReadStringL(ptr);
			TInt nFound = CGlobal::FindGoods(m_pGlobal->g_vZXG, id);
			if (nFound<0)
			{
				TGoodsName gn(id, name->Des());
				m_pGlobal->g_vZXG.Append(gn);
			}
			delete name;
		}

		delete m_strText;
		m_strText = StringLoader::LoadL(LOC_BUF_STR_UPDATEZXGSUC);

		m_strCancel.Copy(KNullDesC);
		if(m_DelayTimer == NULL)
			m_DelayTimer = CPeriodic::NewL(CActive::EPriorityIdle);
		if(!m_DelayTimer->IsActive())
			m_DelayTimer->Start(1000000, 1000000, TCallBack(OnDelay, this));
	}
	return ETrue;
}

void CBlockReadZXG::WriteData(TDes8& aBuf)
{
	CGlobal::WriteString(aBuf,m_pGlobal->m_strUserName);
}

TInt CBlockReadZXG::GetDataLength()
{
	return CGlobal::GetWriteLength(m_pGlobal->m_strUserName);
}

TInt16 CBlockReadZXG::GetDataType()
{
	if (m_bSocketed)
		return RequestDataType_None;
	else
		return RequestDataType_DownloadZXG;
}

TInt CBlockReadZXG::OnDelay(TAny* aAny)
{
	CBlockReadZXG* self = static_cast<CBlockReadZXG*>( aAny );
	self->m_DelayTimer->Cancel();
	CBlockGrid* grid = CBlockGrid::NewL(NULL, CGlobal::GROUP_ZXG, KNullDesC, 0);
	self->GetContainer()->AddBlock(grid);
	return KErrNone;
}
